package com.yunxi.yunxicaiproject.Controller;


import com.yunxi.yunxicaiproject.menu.ToolType;
import com.yunxi.yunxicaiproject.repository.ChatHistoryRepository;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class CustomerController {

    @Resource
    private final ChatClient serverChatClient;
    @Resource
    private ChatHistoryRepository chatHistoryRepository;


    @RequestMapping(value = "/service", produces = "text/html;charset=utf-8")
    public Flux<String> chat(String prompt, String chatId) {
        // 保存会话ID
        chatHistoryRepository.save(ToolType.SERVICE_TOOL.getType(), chatId);

        // 交互
        Flux<String> msg = serverChatClient.prompt()
                .user(prompt)
                .advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
                .stream()
                .content();
        return msg;
    }
}
